package com.ywy.bootadmin.controller.sys;

import com.ywy.bootadmin.common.enums.TaskStatusEnum;
import com.ywy.bootadmin.common.rest.BaseResult;
import com.ywy.bootadmin.common.rest.DataResponse;
import com.ywy.bootadmin.common.rest.PageInfo;
import com.ywy.bootadmin.model.sys.Task;
import com.ywy.bootadmin.service.sys.TaskService;
import com.ywy.bootadmin.util.MapUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 定时任务Controller
 *
 * @author ywy
 * @date 2022/1/7 16:33
 */
@Controller
@RequestMapping("task")
@Slf4j
@Api("定时任务相关接口")
public class TaskController {
    @Autowired
    private TaskService taskService;

    /**
     * 查询定时任务列表数据
     * @param pageInfo
     * @param task
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    @PreAuthorize("hasAuthority('sys:task:query')")
    @ApiOperation("分页查询任务列表")
    public DataResponse list(PageInfo pageInfo, Task task) {
        log.info("TaskController.list(), param: {}, {}", pageInfo, task);

        Map<String, Object> paramsMap = MapUtil.convertToMap(task);
        BaseResult baseResult = taskService.listByPage(pageInfo.getOffset(), pageInfo.getLimit(), paramsMap);
        if (baseResult.isSuccess()) {
            return DataResponse.success(baseResult.getData());
        }
        return DataResponse.failure(baseResult.getError());
    }

    /**
     * 跳转到添加任务页面
     * @return
     */
    @GetMapping("/add")
    @PreAuthorize("hasAuthority('sys:task:add')")
    @ApiOperation("跳转到添加任务页面")
    public String toAdd() {
        return "/task/task-add";
    }

    /**
     * 添加任务信息
     * @param task
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    @PreAuthorize("hasAuthority('sys:task:add')")
    @ApiOperation("添加任务信息")
    public DataResponse add(Task task) {
        log.info("TaskController.add(), param: {}", task);

        BaseResult baseResult = taskService.save(task);
        if (baseResult.isSuccess()) {
            return DataResponse.success(baseResult.getData());
        }
        return DataResponse.failure(baseResult.getError());
    }

    /**
     * 跳转到修改角色页面
     * @return
     */
    @GetMapping("/edit")
    @PreAuthorize("hasAuthority('sys:task:edit')")
    @ApiOperation("跳转到修改角色页面")
    public String toEdit(Model model, Integer id) {
        Task task = taskService.getById(id);
        model.addAttribute("task", task);
        return "/task/task-edit";
    }

    /**
     * 修改任务信息
     * @param task
     * @return
     */
    @PutMapping("/edit")
    @ResponseBody
    @PreAuthorize("hasAuthority('sys:task:edit')")
    @ApiOperation("修改任务信息")
    public DataResponse edit(Task task) {
        log.info("TaskController.edit(), param: {}", task);

        BaseResult baseResult = taskService.update(task);
        if (baseResult.isSuccess()) {
            return DataResponse.success(baseResult.getData());
        }
        return DataResponse.failure(baseResult.getError());
    }

    /**
     * 删任务信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ResponseBody
    @PreAuthorize("hasAuthority('sys:task:del')")
    @ApiOperation("删除任务信息")
    public DataResponse delete(Integer id) {
        log.info("TaskController.delete(), param: {}", id);

        BaseResult baseResult = taskService.delete(id);
        if (baseResult.isSuccess()) {
            return DataResponse.success(baseResult.getData());
        }
        return DataResponse.failure(baseResult.getError());
    }

    /**
     * 批量删除定时任务信息
     * @param ids
     * @return
     */
    @DeleteMapping("/batchDelete")
    @ResponseBody
    @PreAuthorize("hasAuthority('sys:task:del')")
    @ApiOperation("批量删除任务信息")
    public DataResponse batchDelete(@RequestParam("ids[]") Integer[] ids) {
        log.info("TaskController.batchDelete(), param: {}", ids);

        BaseResult baseResult = taskService.batchDelete(ids);
        if (baseResult.isSuccess()) {
            return DataResponse.success(baseResult.getData());
        }
        return DataResponse.failure(baseResult.getError());
    }

    /**
     * 立即执行定时任务
     * @param id
     * @return
     */
    @PostMapping("/run")
    @ResponseBody
    @PreAuthorize("hasAuthority('sys:task:edit')")
    @ApiOperation("立即执行定时任务")
    public DataResponse run(Integer id) {
        log.info("TaskController.run(), param: {}", id);

        BaseResult baseResult = taskService.run(id);
        if (baseResult.isSuccess()) {
            return DataResponse.success(baseResult.getData());
        }
        return DataResponse.failure(baseResult.getError());
    }

    /**
     * 修改定时任务状态
     * @param id
     * @param jobStatus
     * @return
     */
    @PostMapping("/changeStatus")
    @ResponseBody
    @PreAuthorize("hasAuthority('sys:task:edit')")
    @ApiOperation("修改定时任务状态")
    public DataResponse changeStatus(Integer id, Boolean jobStatus) {
        log.info("TaskController.changeStatus(), param: {}, {}", id, jobStatus);

        Integer status = jobStatus ? TaskStatusEnum.RUNNING.getCode() : TaskStatusEnum.STOP.getCode();
        BaseResult baseResult = taskService.changeStatus(id, status);
        if (baseResult.isSuccess()) {
            return DataResponse.success(baseResult.getData());
        }
        return DataResponse.failure(baseResult.getError());
    }
}
